package cn.schoolwow.util.domain.query.jsonarray;

import cn.schoolwow.quickflow.QuickFlowBuilder;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.util.module.query.jsonarray.domain.JSONArrayQueryOption;
import cn.schoolwow.util.module.query.jsonarray.flow.get.GetJSONArrayMultipleFieldValueListBusiness;
import cn.schoolwow.util.module.query.jsonarray.flow.get.GetJSONArraySingleFieldValueListBusiness;
import cn.schoolwow.util.module.query.jsonarray.flow.group.GroupJSONArrayByFieldBusiness;
import cn.schoolwow.util.module.query.jsonarray.service.FilterJSONArrayFlow;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class QueryJSONArrayResponse {
    private JSONArrayQueryOption jsonQueryOption;

    public QueryJSONArrayResponse(JSONArrayQueryOption jsonQueryOption) {
        this.jsonQueryOption = jsonQueryOption;
    }

    /**获取过滤后个数*/
    public long count(){
        JSONArray array = getArray();
        return array.size();
    }

    /**获取过滤后的列表*/
    public JSONObject getObject(){
        JSONArray array = getArray();
        if(null==array||array.size()==0){
            return null;
        }
        return array.getJSONObject(0);
    }

    /**获取过滤后的列表*/
    public JSONObject checkObject(){
        JSONArray array = getArray();
        Map<Collection<String>,Object> conditionMap = new HashMap<>();
        conditionMap.putAll(jsonQueryOption.fieldValueMap);
        conditionMap.putAll(jsonQueryOption.fieldValueInListMap);
        if(null==array||array.size()==0){
            throw new IllegalArgumentException("符合条件的实例为空!" + "查询条件:"+conditionMap);
        }
        if(array.size()>1){
            throw new IllegalArgumentException("期望获取1个实例，实际个数:"+array.size()+"!查询条件:"+conditionMap);
        }
        return array.getJSONObject(0);
    }

    /**获取过滤后的列表*/
    public JSONArray getArray(){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterJSONArrayFlow())
                .putInstanceData(jsonQueryOption)
                .execute();
        JSONArray filterArray = (JSONArray) flowContext.getData("filterArray");
        return filterArray;
    }

    /**获取指定字段的值*/
    public List getSingleFieldValueList(String fieldName){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterJSONArrayFlow())
                .next(new GetJSONArraySingleFieldValueListBusiness())
                .putInstanceData(jsonQueryOption)
                .putCurrentCompositeFlowData("fieldName", fieldName)
                .execute();
        List fieldValueList = (List) flowContext.getData("fieldValueList");
        return fieldValueList;
    }

    /**获取指定多个字段的值*/
    public List getMultipleFieldValueList(String... fieldNames){
        return getMultipleFieldValueList(Arrays.asList(fieldNames));
    }

    /**获取指定多个字段的值*/
    public List getMultipleFieldValueList(Collection<String> fieldNames){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterJSONArrayFlow())
                .next(new GetJSONArrayMultipleFieldValueListBusiness())
                .putInstanceData(jsonQueryOption)
                .putCurrentCompositeFlowData("fieldNames", fieldNames)
                .execute();
        List fieldValueList = (List) flowContext.getData("fieldValueList");
        return fieldValueList;
    }

    /**根据字段字段分组*/
    public JSONObject groupByFieldName(String fieldName){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterJSONArrayFlow())
                .next(new GroupJSONArrayByFieldBusiness())
                .putInstanceData(jsonQueryOption)
                .putCurrentCompositeFlowData("fieldName", fieldName)
                .execute();
        JSONObject fieldJSONArrayMap = (JSONObject) flowContext.getData("fieldJSONArrayMap");
        return fieldJSONArrayMap;
    }
}
